package com.bops.biz.permission;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.bops.service.CommonServiceLocator;
import com.bops.valve.webuser.BopsWebUser;


public class GrantManager {
    /**
     * 查看当前用户是否存在权限
     * @return
     */
    public static boolean hasPermission(String roleId)
    {
        BopsWebUser bopsWebUser = BopsWebUser.getCurrentUser();
        if (bopsWebUser==null)
        {
            return false;
        }
        return bopsWebUser.hasPermission(roleId);
    }
    
    @SuppressWarnings("rawtypes")
    public static UserGrant getUserGrant(Integer usersID,List<String> roleIdList)
    {
        UserGrant userGrant = new UserGrant();
        Map allRoleMap = CommonServiceLocator.getAllRole();
        List<Role> tmpRoleList = new ArrayList<Role>();
        if (allRoleMap!=null)
        {
            for (String key : roleIdList) {
                if (key != null) {
                    Role tmpRole = (Role)allRoleMap.get(key);
                    if (tmpRole != null) {
                        tmpRoleList.add(tmpRole);
                    }
                }
            }
        }
        userGrant.setRoleList(tmpRoleList);
        return userGrant;
    }
    
    
    

    /**
     * 返回Grant
     * 
     * @param grantText
     * @return
     */
    public static Grant getGrant(String grantText) {
        if (grantText == null) {
            return null;
        }
        Grant grant = grantTextMap.get(grantText);
        if (grant == null) {
            grant = new Grant(grantText);
            grantTextMap.put(grantText, grant);
        }
        return grant;
    }
    
    
    private static ConcurrentHashMap<String, Grant> grantTextMap = new ConcurrentHashMap<String, Grant>();
}

